package com.wlts.wzy.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Random;
import java.util.TreeMap;

public class StringUtils extends com.alibaba.druid.util.StringUtils {
    /**
     * StringUtils工具类方法
     * 获取一定长度的随机字符串，范围0-9，a-z
     *
     * @param length：指定字符串长度
     * @return 一定长度的随机字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }

    public static String getRandomIntByLength(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }

    public static boolean isNotEmptyAndZero(Integer cs) {
        return cs != null && cs != 0;
    }

    public static boolean isNotEmpty(Integer cs) {
        return cs != null;
    }

    public static boolean isNotEmpty(Object cs) {
        return cs != null;
    }

    public static boolean isNotEmpty(String cs) {
        return cs != null && !cs.equals("");
    }


    /**
     * Java实体类转Map：方法一
     *
     * @param obj
     * @return
     */
    public static TreeMap<String, String> entityToTreeMap(Object obj) {
        TreeMap<String, String> map = new TreeMap<String, String>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            String object = null;
            try {
                Object object1=field.get(obj);
                if(object1!=null){
                    object = object1.toString();
                }

            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            if(object!=null){
                map.put(fieldName, object);
            }
        }
        return map;
    }

}